package com.firebird.app.reader.pdf.activity

import android.graphics.Bitmap
import android.graphics.pdf.PdfRenderer
import android.os.Bundle
import android.os.ParcelFileDescriptor
import android.widget.ImageView
import com.firebird.app.reader.R
import com.firebird.app.reader.pdf.filehelper.FileConstant
import com.firebird.app.reader.pdf.filehelper.FileUtils
//import com.firebird.app.reader.pdf.R
import java.io.File
import java.io.IOException

class ShowEditedPdfActivity : BaseActivity() {
    private var mFileDescriptor // File descriptor of the PDF.
            : ParcelFileDescriptor? = null
    private var mPdfRenderer: PdfRenderer? = null
    private var mCurrentPage //Page that is currently shown on the screen.
            : PdfRenderer.Page? = null
    private var showEditedIv: ImageView? = null
    override fun onCreate(savedInstanceState: Bundle) {
        super.onCreate(savedInstanceState)
        setContentLayout(R.layout.activity_show_edited_pdf)
        showEditedIv = findViewById(R.id.asep_iv) as ImageView
        showPdf()
    }

    private fun showPdf() {
        val sdPath: String = FileUtils.sDPath
                ?: //没有SD卡不能保存，可以添加提示
                return
        val folderFile = File(sdPath + FileConstant.PDF_PATH)
        if (!folderFile.exists() || !folderFile.isDirectory) {
            return
        }
        val allPdfFiles = folderFile.listFiles()
        if (allPdfFiles == null || allPdfFiles.size < 1) {
            return
        }
        val fileName = intent.getStringExtra("fileName")
        var pdfFile: File? = null
        for (file in allPdfFiles) {
            if (file.name == fileName) {
                pdfFile = file
            }
        }
        if (pdfFile == null) {
            return
        }
        try {
            openRenderer(pdfFile.absolutePath)
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }

    /**
     * Sets up a [android.graphics.pdf.PdfRenderer] and related resources.
     */
    @Throws(IOException::class)
    private fun openRenderer(path: String) { // In this sample, we read a PDF from the assets directory.
        val file = File(path)
        mFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY)
        // This is the PdfRenderer we use to render the PDF.
        if (mFileDescriptor != null) {
            mPdfRenderer = PdfRenderer(mFileDescriptor!!)
        }
        showPage(0)
    }

    /**
     * Shows the specified page of PDF to the screen.
     *
     * @param index The page index.
     */
    private fun showPage(index: Int) {
        if (mPdfRenderer!!.pageCount <= index) {
            return
        }
        // Make sure to close the current page before opening another one.
        if (null != mCurrentPage) {
            mCurrentPage!!.close()
        }
        // Use `openPage` to open a specific page in PDF.
        mCurrentPage = mPdfRenderer!!.openPage(index)
        // Important: the destination bitmap must be ARGB (not RGB).
        val bitmap = Bitmap.createBitmap(mCurrentPage!!.width, mCurrentPage!!.height,
                Bitmap.Config.ARGB_8888)
        // Here, we render the page onto the Bitmap.
// To render a portion of the page, use the second and third parameter. Pass nulls to get
// the default result.
// Pass either RENDER_MODE_FOR_DISPLAY or RENDER_MODE_FOR_PRINT for the last parameter.
        mCurrentPage?.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY)
        // We are ready to show the Bitmap to user.
        showEditedIv!!.setImageBitmap(bitmap)
    }
}